// pages/categoryitem/categoryitem.js
var {getGoodsList} = require('../../api/goods.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goods:[
        ],
    },
    getGoodsListFun(options){
        getGoodsList({cid:options.cid}).then(data=>{
        console.log(data);
        if(data.meta.status == 200){
          let goodslist =data.message.goods;
          let lists = [];
          goodslist.forEach(item=>{
            let{goods_id,goods_name,goods_small_logo,goods_price} = item;
            let data ={
              id:goods_id,
              goodsImage:goods_small_logo,
              goodsName:goods_name,
              goodsAddress:"广州",
              goodsPrice:goods_price == 0 ? "暂无报价":goods_price,   
              goodsPel : goods_price + 600            
            }
            if(goods_small_logo){
              lists.push(data);
            }
          })
          this.setData({
            goods:lists
          })
        }
      })
      },
      goCart(e){
        wx.switchTab({
          url: '/pages/car/car',
        })
        this.getCart(e);
      },
      gopay(e){
        wx.navigateTo({
          url: '/pages/pay/pay',
        })
        this.getCart(e)        
      },
      getCart(e){
      var indexs = e.target.dataset.index
      var CartLists = {
          id:this.data.goods[indexs].id,
          goodsName:this.data.goods[indexs].goodsName,
          goodsPrice:this.data.goods[indexs].goodsPrice,
          goodsImage:this.data.goods[indexs].goodsImage,
          goodsAddress:this.data.goods[indexs].address,
          num:1,
      }
      let GoodsCarLists = wx.getStorageSync('GoodsCarLists');
      if(GoodsCarLists){
          let index = GoodsCarLists.findIndex(item=>{
              return item.id == this.data.goods[indexs].id;
          })
          if(index != -1){
              GoodsCarLists[index].num += 1;
          }else{
              GoodsCarLists.push(CartLists);
          }
          wx.setStorageSync('GoodsCarLists', GoodsCarLists)
      }else{
          wx.setStorageSync('GoodsCarLists', [CartLists])
      }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(options);
        this.getGoodsListFun(options)
        wx.setStorageSync('cid', options.cid)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})